package cn.itcast.mybatis;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.logging.log4j.core.helpers.UUIDUtil;
import org.junit.Before;
import org.junit.Test;

import cn.itcast.po.User;

public class mybatisTest {
	
	private SqlSessionFactory sqlSessionFactory = null ;
	
	@Before
	public void  init() throws  Exception {
		//1. 创建SQLSessionFactoryBuild
		SqlSessionFactoryBuilder sqlSessionFactoryBuilder= new SqlSessionFactoryBuilder();
		//2. 加载配置文件
		InputStream inputStream =Resources.getResourceAsStream("SqlMapConfig.xml");
		//3. 创建SQLSessionFactory
		sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
	}
	

	@Test
	public void getUserById() throws Exception{
		//4. 创建创建SQLSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		//5. 使用创建SQLSession对象执行查询	得到user 对象
			//第一个参数 要执行查询的ID
		User user = sqlSession.selectOne("getUserById",10);
		//6. 打印结果
		System.out.println(user);
		//7. 释放资源
		sqlSession.close();
	}
	
	
	

	@Test
	public void getUserByUsername() throws Exception{
		//4. 创建创建SQLSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		//5. 使用创建SQLSession对象执行查询	得到user 对象
			//第一个参数 要执行查询的ID
		List<User> users = sqlSession.selectList("getUserByName","%张%");
		//6. 遍历打印结果
		for (User user : users) {
			System.out.println(user);
		}
		//7. 释放资源
		sqlSession.close();
	}
	
	/*
	 * 添加用户测试
	 */
	@Test
	public void addUser() throws Exception{
		//4. 创建创建SQLSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		//5. 创建USER对象
		User user = new User();
        user.setUsername("章泽天");
        user.setAddress("北京");
        user.setSex("1");
        user.setBirthday(new Date());
        //6. 插入用户
        sqlSession.insert("insertUser", user);
        System.err.println("id是"+user.getId());
        //7 提交事务
        sqlSession.commit();
		//8. 释放资源
		sqlSession.close();
	}
	
	/*
	 * 添加用户测试
	 */
/*	@Test
	public void addUser2() throws Exception{
		UUID id= UUIDUtil.getTimeBasedUUID();
		//4. 创建创建SQLSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		//5. 创建USER对象
		User user = new User();
        user.setUsername("章泽天");
        user.setAddress("北京");
        user.setSex("1");
        user.setId(id);
        user.setBirthday(new Date());
        //6. 插入用户
        sqlSession.insert("insertUser2", user);
        //7 提交事务
        sqlSession.commit();
		//8. 释放资源
		sqlSession.close();
	}
*/
	
	@Test
	public void deleteUser() throws Exception{
		//4. 创建创建SQLSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		sqlSession.delete("deleteUserById",30);
		//7. 释放资源
		sqlSession.commit();
		sqlSession.close();
	}
	
}
